<?php
/*
	服务公司：沧州讯呗网络科技有限公司
	作者：sun
*/
namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\UsersRepository;
use App\Repositories\UserslogRepository;
use App\Repositories\AddressRepository;
use App\Repositories\PreferlogRepository;
use App\Repositories\PreferRepository;
use App\Repositories\OrderRepository;
use App\Repositories\OrderproductRepository;
use App\Repositories\ContactRepository;
use App\Traits\BaseFun;

class UserController extends Controller
{
    public function __construct(UsersRepository $UsersRepository,UserslogRepository $UserslogRepository,AddressRepository $AddressRepository,PreferlogRepository $PreferlogRepository,PreferRepository $PreferRepository,OrderRepository $OrderRepository,ContactRepository $ContactRepository,OrderproductRepository $OrderproductRepository)
    {
        $this->UsersRepository=$UsersRepository;
        $this->UserslogRepository=$UserslogRepository;
        $this->AddressRepository=$AddressRepository;
        $this->PreferlogRepository=$PreferlogRepository;
        $this->PreferRepository=$PreferRepository;
        $this->OrderRepository=$OrderRepository;
        $this->ContactRepository=$ContactRepository;
        $this->OrderproductRepository=$OrderproductRepository;
    }
    public function UserInfo(Request $request)//用户信息
    {   
        $weiGuo=[];
        $res = $this->UsersRepository->info(['id'=>$request->userId]);
        $address=$this->AddressRepository->info(['userid'=>$request->userId,'state'=>1]);
        if ($address) {
           $res['userAddr']=$address;
        }else{
           $res['userAddr']=$this->AddressRepository->info(['userid'=>$request->userId]);
        }
        $preferList=$this->PreferlogRepository->list(['userid'=>$request->userId,'state'=>2]);
        foreach ($preferList as $key => $value) {
            $preferInfo=$this->PreferRepository->weiGuo(['id'=>$value['preferid'],'end_time'=>date('Y-m-d',time())]);
            if ($preferInfo) {
                $weiGuo[]=$preferInfo;
            }
        }
        $res['prefer']=count($weiGuo);//优惠券总数
        $res['address']=$this->AddressRepository->listCount(['userid'=>$request->userId]);//地址总数
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function UserBalance(Request $request)
    {
        $res['balance']=$this->UsersRepository->info(['id'=>$request->userId])['balance'];
        $res['czzje']=$this->UserslogRepository->logCount(['userid'=>$request->userId,'type'=>1]);
       if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function UserLog(Request $request)//消费记录
    {   
        $res = $this->UserslogRepository->list(['userid'=>$request->userId]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function UserPrefer(Request $request)//优惠券
    {
        $preferList=$this->PreferlogRepository->list(['userid'=>$request->userId,'state'=>2]);
        $weiGuo=[];
        $yiGuo=[];
        foreach ($preferList as $key => $value) {
            $preferInfo=$this->PreferRepository->info(['id'=>$value['preferid']]);
            if ($preferInfo['end_time'] >= date('Y-m-d',time())) {
                $weiGuo[]=$preferInfo;
            }else{
                $yiGuo[]=$preferInfo;
            }
        }
        $res['weiGuo']=$weiGuo;
        $res['yiGuo']=$yiGuo;
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function UserOrder(Request $request)//用户订单
    {   
        $where=[];
        if($request->state){
            $where['state']=$request->state;
        }
        $where['userid']=$request->userId;
        $res=$this->OrderRepository->list($where);
        foreach($res as $k=>$v){
            $res[$k]['orderProduct']=$this->OrderproductRepository->list(['orderid'=>$v['id']]);
            $res[$k]['count']=count($this->OrderproductRepository->list(['orderid'=>$v['id']]));
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function UserOrderInfo(Request $request,$id)//用户订单
    {   
        $res=$this->OrderRepository->info(['id'=>$id]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function Contact(Request $request)
    {
        $res=$this->ContactRepository->info(['id'=>1]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
}
